<template>
  <div class="container">
    <div class="login-con">
      <div class="ivu-card-head">
        <span>欢迎登录</span>
      </div>
      <div class="ivu-card-body">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item prop="username" ref="username">
            <a-input
              v-model="form.username"
              placeholder="账号"
              @blur="
                () => {
                  $refs.username.onFieldBlur();
                }
              "
            >
              <a-icon
                slot="prefix"
                type="user"
                :style="{ color: 'rgba(0,0,0,.25)' }"
              />
            </a-input>
          </a-form-model-item>
          <a-form-model-item prop="password" ref="password">
            <a-input
              v-model="form.password"
              placeholder="密码"
              type="password"
              @blur="
                () => {
                  $refs.password.onFieldBlur();
                }
              "
            >
              <a-icon
                slot="prefix"
                type="lock"
                :style="{ color: 'rgba(0,0,0,.25)' }"
              />
            </a-input>
          </a-form-model-item>

          <a-form-model-item :wrapper-col="{ span: 14, offset: 5 }">
            <a-button @click="resetForm">
              重置
            </a-button>
            <a-button
              type="primary"
              style="margin-left: 25px;"
              @click="onSubmit"
            >
              提交
            </a-button>
          </a-form-model-item>
        </a-form-model>
      </div>
    </div>
  </div>
</template>
<script>
import { login, getUserInfo } from "@/api/Login";
import Cookies from "js-cookie";

export default {
  data() {
    return {
      wrapperCol: { span: 24 },
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {
            required: true,
            message: "账号不允许为空",
            trigger: "blur"
          }
        ],
        password: [
          {
            required: true,
            message: "密码不允许为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.openAccountNotification();
  },
  methods: {
    openAccountNotification() {
      this.$notification.open({
        message: "提示",
        description:
          "管理员账号:张三峰,密码:123  用户账号:李四,密码:123  请不要修改以上2个用户账号密码",
        duration: 10
      });
    },
    openNotification() {
      // 获取当前时间
      let timeNow = new Date();
      // 获取当前小时
      let hours = timeNow.getHours();
      // 设置默认文字
      let text = ``;
      // 判断当前时间段
      if (hours >= 0 && hours <= 10) {
        text = `早上`;
      } else if (hours > 10 && hours <= 14) {
        text = `中午`;
      } else if (hours > 14 && hours <= 18) {
        text = `下午`;
      } else if (hours > 18 && hours <= 24) {
        text = `晚上`;
      }

      this.$notification["success"]({
        message: `${text}好,欢迎回来`
      });
    },
    onSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          login(this.form).then(response => {
            let data = response.data;
            console.log(data);
            if (!data.flag) {
              this.$notification["warning"]({
                message: "提示",
                description: data.message
              });
            } else {
              //登录成功
              this.openNotification();
              // spring Security 的user,包括 username,权限信息
              let user = data.data;
              Cookies.set("user", JSON.stringify(user));

              //用username去获取用户（自己的user表）信息
              getUserInfo(data.data.username).then(response => {
                let data = response.data;
                if (data.flag) {
                  let userInfo = data.data;
                  console.log("userInfo============", userInfo);
                  Cookies.set("userInfo", JSON.stringify(userInfo));
                }
              });

              setTimeout(() => {
                // 前往首页
                this.$router.push("/");
              }, 600);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  }
};
</script>

<style scoped>
.container {
  background-image: url("../assets/login-bg.jpg");
  width: 100%;
  height: 100%;
  background-size: cover;
  background-position: 50%;
  position: relative;
}

.login-con {
  position: absolute;
  right: 290px;
  top: 200px;
  width: 300px;
  background: #fff;
}

.ivu-card-head {
  border-bottom: 1px solid #e8eaec;
  padding: 14px 16px;
}

.ivu-card-head span {
  font-size: 16px;
  color: #17233d;
  font-weight: 700;
  height: 20px;
  line-height: 20px;
  padding-left: 90px;
}

.ivu-card-body {
  padding: 16px;
}
</style>
